#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<map>
#include<cctype>
using namespace std;
bool check(char& ch)
{
	if (isalpha(ch) || isdigit(ch)) return true;
	else return false;
}
int main()
{
	map<string, int> mp;
	int N, P;cin >> N >> P;
	for (int i = 1; i <= N; i++)
	{
		string s1;int l;
		cin >> s1 >> l;
		mp[s1] += l;
	}
	string s2;int count = 0;char ch;
	while (scanf("%c",&ch) != EOF)
	{
		if (check(ch)) s2 += ch;
		else
		{
			count += mp[s2];
			s2 = "";
		}
	}
	cout << count % P << endl;


	return 0;
}